#ifndef RTE_KT1282_SLAVE_BUFQUEUE_H
#define RTE_KT1282_SLAVE_BUFQUEUE_H

#include "logger_rte.h"

template<typename data_type_queue>
class BufQueue {
public:
    int begin(int _size) {
        if (size != _size) {
            size = _size;
            buf = (data_type_queue *)
                    malloc(size * sizeof(data_type_queue));
            memset(buf, 0, size * sizeof(data_type_queue));
            if (!buf) {
                logger.error("out of memory");
                return -1;

            }
            num = 0;
            index = 0;
        }
        return 0;
    }

    int push(data_type_queue c) {
        buf[index] = c;
        index++;
        index %= size;
        return 0;
    }

    data_type_queue avr_value() {
        float sum = 0;
        for (int i = 0; i < size; i++) {
            sum += buf[i];
        }
        return sum / size;
    }

    data_type_queue *buf;
    int size, num, index;

    data_type_queue sum() {
        float sum = 0;
        for (int i = 0; i < size; i++) {
            sum += buf[i];
        }
        return sum;
    }
};

#endif // RTE_KT1282_SLAVE_BUFQUEUE_H